package org.hit.burkun.extract;

import java.util.Collection;
import java.util.HashMap;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.hit.burkun.entities.Entity;

public class FiledsToEntityMapper {
	// 这里先忽略uid
	public enum FILED {
		NAME, DBID, UID, DEF, PRIMARY_ID, OTHER
	};

	private String tbName;
	private Map<FILED, String> fileds;
	private String queryTemplete;
	private FILED queryFiled;
	private boolean isQuote;
	private Map<FILED, Integer> resIndex = new HashMap<FiledsToEntityMapper.FILED, Integer>();
	// entity type
	private Entity.TYPE type;
	// entity fake
	private boolean isFakeEntity;

	/**
	 * 这个构造函数生成的是一个假的entity，不查询数据库
	 * 
	 * @param type
	 */
	public FiledsToEntityMapper(Entity.TYPE type) {
		// 标记一下是否是假的，假的就不从数据库里面查
		isFakeEntity = true;
		this.type = type;
		queryTemplete = "";
		isQuote = false;
		queryFiled = null;
		fileds = new HashMap<>();
	}

	/**
	 * 这个构造函数生成的是真的，要从数据库中查询出信息的
	 * 
	 * @param tbName
	 * @param type
	 * @param fileds
	 * @param queryFiled
	 * @param isQuote
	 */
	public FiledsToEntityMapper(String tbName, Entity.TYPE type,
			Map<FILED, String> fileds, FILED queryFiled, boolean isQuote) {
		super();
		this.tbName = tbName;
		this.fileds = fileds;
		this.queryFiled = queryFiled;
		this.isQuote = isQuote;
		this.type = type;
		isFakeEntity = false;
		this.init();
	}

	private void init() {
		StringBuilder sb = new StringBuilder();
		int idxer = 0;
		sb.append("select ").append(fileds.get(FILED.DBID)).append(",");
		resIndex.put(FILED.DBID, idxer++);
		sb.append(fileds.get(FILED.PRIMARY_ID)).append(",");
		resIndex.put(FILED.PRIMARY_ID, idxer++);
		if (fileds.containsKey(FILED.NAME)) {
			sb.append(fileds.get(FILED.NAME)).append(",");
			resIndex.put(FILED.NAME, idxer++);
		}

		if (fileds.containsKey(FILED.DEF)) {
			sb.append(fileds.get(FILED.DEF)).append(",");
			resIndex.put(FILED.DEF, idxer++);
		}
		if (fileds.containsKey(FILED.UID)) {
			sb.append(fileds.get(FILED.UID)).append(",");
			resIndex.put(FILED.UID, idxer++);
		}
		if (fileds.containsKey(FILED.OTHER)) {
			sb.append(fileds.get(FILED.OTHER)).append(",");
			resIndex.put(FILED.OTHER, idxer++);
		}
		sb.deleteCharAt(sb.length() - 1);
		sb.append(" from ").append(tbName);
		sb.append(" where ").append(fileds.get(queryFiled)).append(" in (%s)");
		queryTemplete = sb.toString();
	}

	public String getQueryTemplete() {
		return queryTemplete;
	}

	public void setQueryTemplete(String queryTemplete) {
		this.queryTemplete = queryTemplete;
	}

	public String buildQueryWithData(Collection<String> data) {
		String datas;
		if (isQuote) {
			datas = "'" + StringUtils.join(data, "','") + "'";
		} else {
			datas = StringUtils.join(data, ",");
		}
		return String.format(queryTemplete, datas);
	}

	public String buildQueryAllData() {
		return queryTemplete.split("where")[0];
	}

	public String buildQueryAllDataWithCondition(String condition) {
		return queryTemplete.split("where")[0] + " " + condition;
	}

	public int getFiledsSize() {
		return fileds.size();
	}

	public int getResultIdxByFiled(FILED fd) {
		if (resIndex.containsKey(fd)) {
			return resIndex.get(fd);
		}
		return -1;
	}

	public Entity.TYPE getType() {
		return this.type;
	}

	public String getTbName() {
		return this.tbName;
	}

	public boolean isFake() {
		return isFakeEntity;
	}
}
